package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    /*
    * 查询今日运营数据
    * */
    public BusinessDataVO businessData(LocalDateTime begin,LocalDateTime end) {
        // 查询新增用户数
        Integer newUsers = defaultVal(userMapper.getUserTotalByDate(begin, end),0);
        Map map = new HashMap();
        map.put("beginTime",begin);
        map.put("endTime",end);
        // 今日总订单数量
        Integer orderTotal = defaultVal( orderMapper.selectOrderStatusCount(map),0);
        map.put("status", Orders.COMPLETED);
        // 今日已完成订单数
        Integer validOrderCount = defaultVal(orderMapper.selectOrderStatusCount(map),0);
        // 订单完成率

        Double orderCompletionRate =orderTotal == 0.00 ? 0.00 :  Double.parseDouble(new DecimalFormat("0.00")
                .format(defaultVal((validOrderCount / orderTotal),0.00)));
        Double turnover = (Double) defaultVal(orderMapper.returnOverMoneyByDay(map),0.00);
        Double unitPrice = Double.valueOf(new DecimalFormat("0.00").format(defaultVal(orderMapper.getCompleteOrdersAVGAmount(map),0.00)));
        return BusinessDataVO.builder()
                .turnover(turnover)
                .newUsers(newUsers)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .build();
    }
    /*
    * 查询套餐总览
    * */
    public SetmealOverViewVO overviewSetmeals() {
        Integer enableSetmealCount = setmealMapper.getSetmealStatusCount(StatusConstant.ENABLE);
        Integer disableSetmealCount = setmealMapper.getSetmealStatusCount(StatusConstant.DISABLE);
        return SetmealOverViewVO.builder()
                .sold(enableSetmealCount)
                .discontinued(disableSetmealCount)
                .build();
    }
    /*
    * 查询菜品总览
    * */
    public DishOverViewVO overviewDishes() {
        Integer enableDishCount = dishMapper.getDishStatusCount(StatusConstant.ENABLE);
        Integer disableDishCount = dishMapper.getDishStatusCount(StatusConstant.DISABLE);
        return DishOverViewVO.builder()
                .sold(enableDishCount)
                .discontinued(disableDishCount)
                .build();
    }
    /*
    * 查询订单管理数据
    * */
    public OrderOverViewVO overviewOrders() {
        Map map = new HashMap();
        Integer allOrders =  orderMapper.selectOrderStatusCount(map);
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.selectOrderStatusCount(map);
        map.put("status",Orders.COMPLETED);
        Integer completedOrders = orderMapper.selectOrderStatusCount(map);
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.selectOrderStatusCount(map);
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.selectOrderStatusCount(map);
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

    public <T> T defaultVal(T obj, T defaultVal){
        return  obj == null ? defaultVal : obj;
    }
}
